package com.javandad.app.client.toppanel;

import java.util.LinkedList;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.javandad.app.shared.entity.Friendship;
import com.javandad.app.shared.entity.Message;

public class Main implements EntryPoint {

	private final TopPanelServiceAsync toppanelService = GWT
			.create(TopPanelService.class);

	@Override
	public void onModuleLoad() {
		initGUI();
		getSessionInfo();

	}

	private void initGUI() {
		// friendshipStatus.setText(session.get(0));

		initDialog();

		sendMessageButton.setVisible(true);
		friendshipStatus.setVisible(true);
		addFriendButton.setVisible(false);
		
		panel.add(sendMessageButton);
		panel.add(addFriendButton);
		panel.add(friendshipStatus);

		sendMessageButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				newMessageDialog.show();

			}
		});

		final String view = com.google.gwt.user.client.Window.Location
				.getParameter("view");

		if (view == null)
			panel.setVisible(false);
		else
			panel.setVisible(true);

		addFriendButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				toppanelService.addAsFriend(new Friendship(session.get(1),
						view, null, null), new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Adding failed!");
					}

					@Override
					public void onSuccess(Boolean result) {
						if (result) {
							// friendshipStatus.setText("Waiting for " + view);
							addFriendButton.setEnabled(false);
							addFriendButton.setText("Waiting...");
						}
					}

				});

			}
		});

		RootPanel.get("topPanel").add(panel);

	}

	private void initDialog() {
		to.setWidth("590px");
		to.setText(com.google.gwt.user.client.Window.Location
				.getParameter("view"));
		to.setReadOnly(true);

		subject.setWidth("590px");

		content.setWidth("590px");
		content.setHeight("150px");

		cancelButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				newMessageDialog.hide();
				subject.setText("");
				content.setText("");
			}
		});

		sendButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				sendMessage();
			}
		});

		mpanel.setSize("600px", "300px");

		mpanel.add(tol);
		mpanel.add(to);
		mpanel.add(subjectl);
		mpanel.add(subject);
		mpanel.add(contentl);
		mpanel.add(content);
		mpanel.add(cancelButton);
		mpanel.add(sendButton);

		newMessageDialog.setTitle("Write a new message");
		newMessageDialog.setAnimationEnabled(true);

		newMessageDialog.add(mpanel);
		newMessageDialog.setPopupPosition(400, 200);
	}

	private void getSessionInfo() {
		toppanelService.getSessionInfo(new AsyncCallback<List<String>>() {

			@Override
			public void onSuccess(List<String> result) {
				Main.this.session = result;
				String view = com.google.gwt.user.client.Window.Location
				.getParameter("view");
				if ( session.get(1) != null && view != null)
				toppanelService.checkRelationshipStatus(session.get(1),view
						,
						new AsyncCallback<Integer>() {

							@Override
							public void onSuccess(Integer result) {
								switch (result) {
								case 0:
									addFriendButton.setVisible(true);
									break;
								case 1:
									addFriendButton.setText("Waiting...");
									addFriendButton.setEnabled(false);
									addFriendButton.setVisible(true);
									break;
								case 2:
									
									addFriendButton.setVisible(false);
									break;


								}

							}

							@Override
							public void onFailure(Throwable caught) {
							}
						});

			}

			@Override
			public void onFailure(Throwable caught) {
			}
		});
	}

	private void sendMessage() {

		String sender = "anonymous";
		if (session.size() > 1 && session.get(1) != null)
			sender = session.get(1).toString();

		toppanelService.sendMessage(new Message(sender, to.getText(), content
				.getText(), subject.getText(), null),
				new AsyncCallback<Boolean>() {
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Message Failed!");
					}

					@Override
					public void onSuccess(Boolean result) {
						if (result) {
							Window.alert("Message Sent!");

							subject.setText("");
							content.setText("");
							newMessageDialog.hide();
						} else
							Window.alert("Message Failed!");

					}

				});

	}

	final private HorizontalPanel panel = new HorizontalPanel();
	final private Button sendMessageButton = new Button("send a message");
	final private Button addFriendButton = new Button("add as a friend");
	final private Label friendshipStatus = new Label();
	final private DialogBox newMessageDialog = new DialogBox();
	private List<String> session = new LinkedList<String>();

	// Message Dialog Components
	private Label tol = new Label("To");
	private TextBox to = new TextBox();
	private Label subjectl = new Label("Subject");
	private TextBox subject = new TextBox();
	private Label contentl = new Label("Content");
	private RichTextArea content = new RichTextArea();
	private Button cancelButton = new Button("Cancel");
	private Button sendButton = new Button("Send");
	private AbsolutePanel mpanel = new AbsolutePanel();

}
